import numpy as np
import cv2
from scipy.ndimage import distance_transform_edt as distance
from skimage import segmentation as sk_seg

file = "/home/jiayu/MyProject_2022/data/Dataset(612)/cell/BHR(31).png"
image = cv2.imread(file, flags=-1)
posmask = image.astype(bool)
if posmask.any():
    negmask = ~posmask
    posdis = distance(posmask)
    negdis = distance(negmask)
    boundary = sk_seg.find_boundaries(posmask, mode='inner').astype(np.uint8)
    sdf = (negdis - np.min(negdis)) / (np.max(negdis) - np.min(negdis)) - (posdis - np.min(posdis)) / (
                np.max(posdis) - np.min(posdis))
    sdf[boundary==1]=0

cv2.imwrite("/home/jiayu/MyProject_2022/data/Dataset(612)/BHR(31).png", sdf*255)
